<?php namespace Admin\Controller;
use Common\Model\Shop_attribute;
use Common\Model\Attribute;
class Shop_listController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Shop_list;
	}
	public function index(){

		$sid=Q('get.sid',0,'intval');
//		添加货品
		if(IS_POST){
//			p($_POST);exit;
		if($this->model->store()) View::success('添加成功');
		}
		 //处理表格表头的规格的数据例如:"颜色、尺码"
		$shop_attributeModel=new Shop_attribute;
		$data=$shop_attributeModel
			 ->join('attribute','attribute_aid','=','aid')
			 ->where("shop_id={$sid} AND is_recycle=1")
			 ->groupBy('aid')
			 ->get();
//		p($data);exit;
		//添加货品的时候下拉菜单所选择的内容(一定是从商品属性表获得)
			 
		foreach($data as $k=>$v){
			$data[$k]['value']=$shop_attributeModel->where("attribute_aid={$v['aid']} AND shop_id={$sid}")->get();
		}
//		p($data);
		View::with('data',$data);

        $listData = $this->model->where("sid={$sid}")->get();
//		p($listData);exit;
		
        foreach ($listData as $k => $v) {
            $listData[$k]['value'] = $shop_attributeModel->where("sid IN({$v['combine']})")->groupBy('attribute_aid')->get();
        }
//		p($listData);exit;
        View::with('listData',$listData);
		
		View::make();
		
	}
	
	//	编辑
	public function edit(){
		$sid=Q('get.sid',0,'intval');
		$slid=Q('get.slid',0,'intval');
		if(IS_POST){
			$this->model->edit();
		}
//		获取旧数据
		$oldData=$this->model->where("slid={$slid}")->find();
		View::with('oldData',$oldData);
//		p($oldData);
		$id=explode(',', $oldData['combine']);
		View::with('id',$id);
		//处理表格表头的规格的数据例如:"颜色、尺码"
		$shop_attributeModel=new Shop_attribute;
		$data=$shop_attributeModel
			 ->join('attribute','attribute_aid','=','aid')
			 ->where("shop_id={$sid} AND is_recycle=1") 
			 ->groupBy('aid')
			 ->get();
//		p($data);exit;
		//添加货品的时候下拉菜单所选择的内容(一定是从商品属性表获得)	 
		foreach($data as $k=>$v){

			$data[$k]['value']=$shop_attributeModel->where("attribute_aid={$v['aid']} AND shop_id={$sid}")->get();
		}
//		p($data);
		View::with('data',$data);

        $listData = $this->model->where("sid={$sid}")->get();
//		p($listData);exit;
		
        foreach ($listData as $k => $v) {
            $listData[$k]['value'] = $shop_attributeModel->where("sid IN({$v['combine']})")->groupBy('attribute_aid')->get();
        }
//		p($listData);exit;
//      分配变量
        View::with('listData',$listData);
		//显示模板
		View::make();
	}
	//	删除
	public function del(){
		$slid=Q('get.slid',0,'intval');
		$this->model->where("slid={$slid}")->delete();
		View::success('删除成功');
	}
}
